package achi.community.irla.beans;

import achi.community.irla.database.DatabaseImpl;
import achi.community.irla.database.ModelBean;
import achi.community.irla.entities.Users;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.CustomScoped;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

/**
 * <p>The TextFieldsBean class is the backing bean for the Text Entry
 * demonstration. It is used to store the values of the input fields.</p>
 */

@ManagedBean
@SessionScoped
//@ManagedBean(name="settingsBackBean")
//@CustomScoped(value = "#{window}")
//@ManagedBean
//@RequestScoped
public class SettingsBackBean implements Serializable{
    /**
     * The different kinds of text input fields.
     */

    //Settings
    @ManagedProperty(value = "#{modelBean}")
    ModelBean modelBean;

    @EJB
    DatabaseImpl db;

    private String firstname;
    private String lastname;
    private String username;
    private String email;

    public void setUsername(String username) {
        this.username = username;
    }

    
    public void setModelBean(ModelBean modelBean) {
        this.modelBean = modelBean;
    }

    public ModelBean getModelBean() {
        return modelBean;
    }

    public String getUsername() {
      //  username = modelBean.getUser().getUsername();
        return username;
    }

    public String getFirstname() {
      //  firstname = modelBean.getUser().getFirstName();
        return firstname;//modelBean.getFirstname();
    }

    public String getLastname() {
     //   lastname = modelBean.getUser().getLastName();
        return lastname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
     //   email = modelBean.getUser().getEmail();
        return email;
    }

    public String fillFields() {
        Users user = modelBean.getUser();
        firstname = user.getFirstName();
        lastname = user.getLastName();
        username = user.getUsername();
        email = user.getEmail();

        return "settings";
    }

    public void buttonClicked() {//ActionEvent evt) {
        // System.out.println("buttonClicked!(1)");

        if (true) { // check if fields is OK
            System.out.println("fname:#"+firstname+"#");
            Users user = modelBean.getUser();

            user.setFirstName(firstname);
            user.setLastName(lastname);
            user.setUsername(username);
            user.setEmail(email);

            db.updateUser(user);
            modelBean.setUser(user);
        }
            /**
        }
            String fname = backingBean.getFirstname();
            System.out.println("fname:#"+fname+"#");
            String lname = backingBean.getLastname();
            String uname = backingBean.getUsername();
            String email = backingBean.getEmail();

            Users user = modelBean.getUser();
            user.setFirstName(fname);
            user.setLastName(lname);
            user.setUsername(uname);
            user.setEmail(email);
*/
            //db.updateUser(user);
            //modelBean.setUser(user);
        }
}